<?php

namespace app\api\controller\Product;

use think\Controller;
use think\Request;

class Cart extends Controller
{

    public function __construct()
    {
        parent::__construct();

        $this->CartModel = model('Product.Cart');
    }

    //购物清单
    public function cartList()
    {
        $userid = $this->request->param('userid', null);
        $Cart = $this->CartModel->where('cart.userid', $userid)->with(['product'])->select();
        if (!$Cart) {
            $this->error('购物车空空，先去添加商品');
        }
        $this->success('查询成功', null, $Cart);
    }

    //添加购物车商品
    public function add()
    {
        $userid = $this->request->param('userid');
        $proid = $this->request->param('proid');
        $num = $this->request->param('num');

        $product = model('Product.Product')->find($proid);

        if ($product['stock'] <= 0) {
            $this->error('该商品库存不足');
        }

        $data = [
            'userid' => $userid,
            'proid' => $proid,
            'nums' => $num,
            'price' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
            'total' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
        ];

        $Cart = $this->CartModel->where(['userid' => $userid, 'proid' => $proid])->find();
        $result = null;
        if ($Cart) {
            $data['id'] = $Cart['id'];
            $data['nums'] = $data['nums'] + $Cart['nums'];
            $data['total'] = (string)($data['nums'] * $data['price']);
            $result = $this->CartModel->Validate('common/Cart')->isUpdate(true)->save($data);
        } else {
            $result = $this->CartModel->Validate('common/Cart')->save($data);
        }
        if ($result === FALSE) {
            $this->error('添加购物车失败');
        } else {
            $this->success('添加购物车成功', null, $this->CartModel->id);
        }
    }

    //商品数量修改
    public function nums()
    {
        $nums = $this->request->param('nums', 0);
        $id = $this->request->param('id', 0);
        $cart = $this->CartModel->find($id);

        if (!$cart) {
            $this->error('该商品未加入购物车里');
        }

        $data = [
            'id' => $id,
            'nums' => $nums,
            'total' => bcmul($cart['price'], $nums)
        ];

        $result = $this->CartModel->isUpdate(true)->save($data);

        if ($result === FALSE) {
            $this->error('更新该商品数量失败');
        } else {
            $this->success('更新成功');
        }
    }

    //商品删除
    public function del()
    {
        $cartId = $this->request->param('cartid', 0);
        $cart = $this->CartModel->find($cartId);
        if (!$cart) {
            $this->error('没有该商品');
        }
        $result = $this->CartModel->where('id', $cartId)->delete();
        if ($result === FALSE) {
            $this->error('删除该商品失败');
        } else {
            $this->success('删除成功');
        }
    }

    //选中商品的订单信息
    public function listIndex()
    {
        $cartids = $this->request->param('ids');

        $list = $this->CartModel->with(['product'])->whereIn('cart.id', $cartids)->select();

        if (!$list) {
            $this->error('所选的商品不存在，请重新选择');
        }

        $this->success('查询成功', null, $list);
    }

    //立即购买的购物车逻辑
    public function addId()
    {
        $userid = $this->request->param('userid');
        $proid = $this->request->param('proid');
        $num = $this->request->param('num');

        $product = model('Product.Product')->find($proid);

        if ($product['stock'] <= 0) {
            $this->error('该商品库存不足');
        }

        $product = model('Product.Product')->find($proid);

        if ($product['stock'] <= 0) {
            $this->error('该商品库存不足');
        }

        $data = [
            'userid' => $userid,
            'proid' => $proid,
            'nums' => $num,
            'price' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
            'total' => $product['vip_price'] > 0 ? $product['vip_price'] : $product['price'],
        ];
        $data['total'] = (string)($data['nums'] * $data['price']);
        $result = $this->CartModel->Validate('common/Cart')->save($data);
        if ($result === FALSE) {
            $this->error('添加购物车失败');
        } else {
            $this->success('添加购物车成功', null, $this->CartModel->id);
        }
    }
}
